<template>
  <div class="kanban-management-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">看板管理</h1>
      <p class="page-description">管理和配置生产看板，实时监控生产状态</p>
    </div>



    <!-- 看板列表区域 -->
    <div class="kanban-section">
      <div class="section-header">
        <h2 class="section-title">看板列表</h2>
        <button class="btn btn-primary add-kanban-btn" @click="showAddKanbanModal" title="添加看板">
          <span class="btn-icon">+</span>
        </button>
      </div>

      <!-- 看板卡片网格 -->
      <div class="kanban-grid">
        <!-- 已配置的看板 -->
        <div v-for="kanban in configuredKanbans" :key="kanban.id" class="kanban-card configured" :class="kanban.status"
          :style="{ borderLeftColor: kanban.color, borderLeftWidth: '4px' }" @click="openKanban(kanban)">
          <div class="card-icon">
            <div class="chart-icon">
              <div class="bar bar-1"></div>
              <div class="bar bar-2"></div>
              <div class="bar bar-3"></div>
            </div>
          </div>
          <div class="card-content">
            <h3 class="card-title">{{ kanban.name }}</h3>
            <p class="card-description">{{ kanban.description }}</p>
            <div class="card-status">
              <span class="status-badge" :class="kanban.status">
                {{ getStatusText(kanban.status) }}
              </span>
            </div>
          </div>
          <div class="card-actions">
            <button class="action-btn edit-btn" @click.stop="editKanban(kanban)" title="编辑">
              ✏️
            </button>
            <button class="action-btn delete-btn" @click.stop="deleteKanban(kanban.id)" title="删除">
              🗑️
            </button>
          </div>
        </div>


      </div>
    </div>

    <!-- 添加/编辑看板模态框 -->
    <div v-if="showModal" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ isEditing ? '编辑看板' : '添加看板' }}</h3>
          <button class="close-btn" @click="closeModal">×</button>
        </div>
        <div class="modal-body">
          <form @submit.prevent="saveKanban" class="kanban-form">
            <div class="form-group">
              <label for="kanbanName">看板名称</label>
              <input id="kanbanName" v-model="kanbanForm.name" type="text" placeholder="请输入看板名称" required />
            </div>
            <div class="form-group">
              <label for="kanbanDescription">看板描述</label>
              <textarea id="kanbanDescription" v-model="kanbanForm.description" placeholder="请输入看板描述"
                rows="3"></textarea>
            </div>
            <div class="form-group">
              <label for="kanbanType">看板类型</label>
              <select id="kanbanType" v-model="kanbanForm.type" required>
                <option value="">请选择看板类型</option>
                <option value="production">生产管控</option>
                <option value="workorder">工单执行</option>
                <option value="quality">质量监控</option>
                <option value="inventory">库存管理</option>
                <option value="maintenance">设备维护</option>
                <option value="custom">自定义</option>
              </select>
            </div>
            <div class="form-group">
              <label for="kanbanColor">看板颜色</label>
              <div class="color-picker">
                <div v-for="color in colorOptions" :key="color.value" class="color-option"
                  :class="{ active: kanbanForm.color === color.value }" :style="{ backgroundColor: color.value }"
                  @click="kanbanForm.color = color.value"></div>
              </div>
            </div>
            <div class="form-actions">
              <button type="button" class="btn btn-secondary" @click="closeModal">取消</button>
              <button type="submit" class="btn btn-primary">
                {{ isEditing ? '保存' : '添加' }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>

    <!-- 确认删除对话框 -->
    <div v-if="showDeleteConfirm" class="modal-overlay" @click="cancelDelete">
      <div class="modal-content delete-confirm" @click.stop>
        <div class="modal-header">
          <h3>确认删除</h3>
        </div>
        <div class="modal-body">
          <p>确定要删除看板 "{{ kanbanToDelete?.name }}" 吗？此操作不可恢复。</p>
        </div>
        <div class="modal-actions">
          <button class="btn btn-secondary" @click="cancelDelete">取消</button>
          <button class="btn btn-danger" @click="confirmDelete">确认删除</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'

// 看板数据接口
interface Kanban {
  id: string
  name: string
  description: string
  type: string
  color: string
  status: 'active' | 'inactive' | 'maintenance'
  createdAt: string
  updatedAt: string
}

// 响应式数据
const showModal = ref(false)
const isEditing = ref(false)
const showDeleteConfirm = ref(false)
const kanbanToDelete = ref<Kanban | null>(null)

// 看板表单数据
const kanbanForm = reactive({
  name: '',
  description: '',
  type: '',
  color: '#3B82F6'
})

// 已配置的看板
const configuredKanbans = ref<Kanban[]>([
  {
    id: '1',
    name: '车间生产管控',
    description: '实时监控车间生产状态和进度',
    type: 'production',
    color: '#F59E0B',
    status: 'active',
    createdAt: '2024-01-01',
    updatedAt: '2024-01-01'
  },
  {
    id: '2',
    name: '工单执行进度',
    description: '跟踪工单执行状态和完成情况',
    type: 'workorder',
    color: '#3B82F6',
    status: 'active',
    createdAt: '2024-01-01',
    updatedAt: '2024-01-01'
  }
])



// 颜色选项
const colorOptions = [
  { value: '#3B82F6', label: '蓝色' },
  { value: '#F59E0B', label: '橙色' },
  { value: '#10B981', label: '绿色' },
  { value: '#EF4444', label: '红色' },
  { value: '#8B5CF6', label: '紫色' },
  { value: '#F97316', label: '橙红色' },
  { value: '#06B6D4', label: '青色' },
  { value: '#84CC16', label: '青绿色' }
]



// 方法
const showAddKanbanModal = () => {
  isEditing.value = false
  resetForm()
  showModal.value = true
}

const editKanban = (kanban: Kanban) => {
  isEditing.value = true
  kanbanForm.name = kanban.name
  kanbanForm.description = kanban.description
  kanbanForm.type = kanban.type
  kanbanForm.color = kanban.color
  showModal.value = true
}

const closeModal = () => {
  showModal.value = false
  resetForm()
}

const resetForm = () => {
  kanbanForm.name = ''
  kanbanForm.description = ''
  kanbanForm.type = ''
  kanbanForm.color = '#3B82F6'
}

const saveKanban = () => {
  if (isEditing.value) {
    // 编辑现有看板
    const index = configuredKanbans.value.findIndex(k => k.name === kanbanForm.name)
    if (index !== -1) {
      configuredKanbans.value[index] = {
        ...configuredKanbans.value[index],
        name: kanbanForm.name,
        description: kanbanForm.description,
        type: kanbanForm.type,
        color: kanbanForm.color,
        updatedAt: new Date().toISOString().split('T')[0]
      }
    }
  } else {
    // 添加新看板
    const newKanban: Kanban = {
      id: Date.now().toString(),
      name: kanbanForm.name,
      description: kanbanForm.description,
      type: kanbanForm.type,
      color: kanbanForm.color,
      status: 'active',
      createdAt: new Date().toISOString().split('T')[0],
      updatedAt: new Date().toISOString().split('T')[0]
    }
    configuredKanbans.value.push(newKanban)
  }

  closeModal()
}

const deleteKanban = (kanbanId: string) => {
  const kanban = configuredKanbans.value.find(k => k.id === kanbanId)
  if (kanban) {
    kanbanToDelete.value = kanban
    showDeleteConfirm.value = true
  }
}

const confirmDelete = () => {
  if (kanbanToDelete.value) {
    const index = configuredKanbans.value.findIndex(k => k.id === kanbanToDelete.value!.id)
    if (index !== -1) {
      configuredKanbans.value.splice(index, 1)
    }
    cancelDelete()
  }
}

const cancelDelete = () => {
  showDeleteConfirm.value = false
  kanbanToDelete.value = null
}

const openKanban = (kanban: Kanban) => {
  // 根据看板类型在新标签页中打开不同页面
  switch (kanban.type) {
    case 'production':
      // 车间生产管控 -> Workshop_production页面
      window.open('/Workshop_production', '_blank')
      break
    case 'workorder':
      // 工单执行进度 -> Work_order_progress页面
      window.open('/Work_order_progress', '_blank')
      break
    default:
      // 其他类型暂时显示提示
      console.log('打开看板:', kanban.name)
      alert(`正在开发 ${kanban.name} 页面`)
  }
}

const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    active: '运行中',
    inactive: '已停用',
    maintenance: '维护中'
  }
  return statusMap[status] || status
}

// 生命周期
onMounted(() => {
  // 设置页面标题
  //document.title = '看板管理 - MES系统'
  // 可以在这里加载看板数据
})
</script>

<style scoped>
.kanban-management-container {
  padding: 24px;
  background-color: #f8fafc;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 24px;
  text-align: center;
}

.page-title {
  font-size: 32px;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 8px;
}

.page-description {
  font-size: 16px;
  color: #64748b;
  margin: 0;
}



.kanban-section {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.section-title {
  font-size: 24px;
  font-weight: 600;
  color: #1e293b;
  margin: 0;
}

.add-kanban-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  background: none;
  color: #3b82f6;
  border: none;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
}

.add-kanban-btn:hover {
  transform: scale(1.1);
  color: #1d4ed8;
}

.add-kanban-btn:active {
  transform: scale(0.95);
}

.add-kanban-btn:focus {
  outline: none;
  color: #1d4ed8;
}

.btn-icon {
  font-size: 32px;
  font-weight: 300;
  line-height: 1;
  transition: all 0.2s ease;
}

.kanban-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.kanban-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 2px solid transparent;
  position: relative;
  overflow: hidden;
  animation: fadeInUp 0.6s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }

  to {
    transform: rotate(360deg);
  }
}

.kanban-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.kanban-card.configured:hover {
  transform: translateY(-4px) scale(1.02);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}



.kanban-card.configured {
  border-color: #e2e8f0;
  border-left: 4px solid;
  position: relative;
}

.kanban-card.configured::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
  pointer-events: none;
  border-radius: 12px;
}



.card-icon {
  margin-bottom: 16px;
  display: flex;
  justify-content: center;
}

.chart-icon {
  display: flex;
  align-items: end;
  gap: 4px;
  height: 40px;
}

.bar {
  width: 8px;
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  border-radius: 2px;
}

.bar-1 {
  height: 20px;
}

.bar-2 {
  height: 30px;
}

.bar-3 {
  height: 40px;
}



.card-content {
  text-align: center;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
  margin: 0 0 8px 0;
}

.card-description {
  font-size: 14px;
  color: #64748b;
  margin: 0 0 16px 0;
  line-height: 1.5;
}

.card-status {
  margin-bottom: 16px;
}

.status-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
}

.status-badge.active {
  background: #dcfce7;
  color: #166534;
}

.status-badge.inactive {
  background: #fef2f2;
  color: #991b1b;
}

.status-badge.maintenance {
  background: #fef3c7;
  color: #92400e;
}

.card-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.kanban-card:hover .card-actions {
  opacity: 1;
}

.action-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 0.2s ease;
}

.edit-btn {
  background: #dbeafe;
  color: #1d4ed8;
}

.edit-btn:hover {
  background: #3b82f6;
  color: white;
}

.delete-btn {
  background: #fee2e2;
  color: #dc2626;
}

.delete-btn:hover {
  background: #ef4444;
  color: white;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #1e293b;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #64748b;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: #f1f5f9;
  color: #1e293b;
}

.modal-body {
  padding: 24px;
}

.kanban-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-size: 14px;
  font-weight: 500;
  color: #374151;
}

.form-group input,
.form-group textarea,
.form-group select {
  padding: 12px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.2s ease;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.color-picker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.color-option {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.2s ease;
}

.color-option:hover {
  transform: scale(1.1);
}

.color-option.active {
  border-color: #1e293b;
  transform: scale(1.1);
}

.form-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 8px;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-primary {
  background: #3b82f6;
  color: white;
}

.btn-primary:hover {
  background: #2563eb;
}

.btn-secondary {
  background: #f1f5f9;
  color: #374151;
}

.btn-secondary:hover {
  background: #e2e8f0;
}

.btn-danger {
  background: #ef4444;
  color: white;
}

.btn-danger:hover {
  background: #dc2626;
}

/* 删除确认对话框 */
.delete-confirm .modal-body {
  text-align: center;
}

.delete-confirm .modal-body p {
  margin: 0;
  color: #64748b;
  line-height: 1.6;
}

.modal-actions {
  padding: 20px 24px;
  border-top: 1px solid #e2e8f0;
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .kanban-management-container {
    padding: 16px;
  }



  .kanban-grid {
    grid-template-columns: 1fr;
  }

  .section-header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }

  .add-kanban-btn {
    width: 44px;
    height: 44px;
  }

  .btn-icon {
    font-size: 28px;
  }

  .modal-content {
    width: 95%;
    margin: 20px;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 24px;
  }

  .section-title {
    font-size: 20px;
  }



  .add-kanban-btn {
    width: 40px;
    height: 40px;
  }

  .btn-icon {
    font-size: 24px;
  }

  .kanban-card {
    padding: 16px;
  }
}
</style>